using UnityEngine;
using System.Collections;
using System;

public class Bullet : MonoBehaviour {
	
	static public float s_TimeBetweenChange = 0.05f;
	
	public float minX;
	public float maxX;
	public float ValZ = 67;
	public float vitesseBullet = 5;

	public int LifeTimeSec = 5;
	
	public float StartingTime;

	private TimeSpan m_lifeTime;
	private DateTime m_previousTime;

	private Quaternion keepRotation;
	private bool m_sensRight = false;
	
	private bool m_playerOwned = true;
	
	private float m_TimeSinceLastChange;
	
	private bool m_alreadyHaveBeenInvisible;
	
	public bool acc_playerOwned
	{
		get
		{
			return m_playerOwned;
		}
		set
		{
			m_playerOwned = value;
		}
	}
	
	public bool acc_sensRight
	{
		get
		{
			return this.m_sensRight;
		}
		set
		{
			this.m_sensRight = value;
			
			if(!m_sensRight)
				transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
		}
	}

	public void Start()
	{
		keepRotation = this.transform.rotation;
		this.transform.position = new Vector3(this.transform.position.x, this.transform.position.y, ValZ);
		this.m_lifeTime = new TimeSpan(0, 0, LifeTimeSec);
		m_previousTime = DateTime.Now;
		
		m_TimeSinceLastChange = 0;
		m_alreadyHaveBeenInvisible = false;
		
		StartingTime = Time.time;
	}


	public void destruction()
	{
		Destroy(this.gameObject);
	}


	void OnBecameInvisible()
	{
		/*if(m_alreadyHaveBeenInvisible)
		{
			destruction();
			return;
		}*/
		
		if (m_sensRight)
		{
			//bug.Log("sens droite " + minX);
			this.transform.position = new Vector3(minX, this.transform.position.y, ValZ);
		}
		else
		{
			//Debug.Log("sens gauche " + maxX);
			this.transform.position = new Vector3(maxX, this.transform.position.y, ValZ);
		}
		
		m_alreadyHaveBeenInvisible = true;
		
	}

	void OnTriggerEnter(Collider other)
	{
		if(other.GetComponent<Ennemy>() != null)
		{
			if(m_playerOwned)
				Destroy(this.gameObject);
		}
		else if(other.GetComponent<player>() != null)
		{
			LevelGenerator.sTime -= 1;
			other.SendMessage("OnHit");
			Destroy(this.gameObject);
		}
		else
		{
			if(other.tag == "Shield")
			{	
				if(Time.time - StartingTime < 1.0f)
					return;
			}
			
			Destroy(gameObject);
		}
	}


	// Update is called once per frame
	void Update () 
	{
		this.m_lifeTime -= DateTime.Now.Subtract(m_previousTime);
		
		m_TimeSinceLastChange += Time.deltaTime;
		if(m_TimeSinceLastChange > s_TimeBetweenChange)
		{
			m_TimeSinceLastChange = 0;
			transform.localScale = new Vector3(transform.localScale.x, -transform.localScale.y, transform.localScale.z);
		}
		
		if (this.m_lifeTime <= TimeSpan.Zero)
		{
			Destroy(this.gameObject);
		}
		else
		{
			this.transform.rotation = keepRotation;
			if (m_sensRight)
			{
				this.transform.position = new Vector3(this.transform.position.x + (vitesseBullet*Time.deltaTime), this.transform.position.y, ValZ);
			}
			else
			{
				this.transform.position = new Vector3(this.transform.position.x - ((vitesseBullet+LevelGenerator.sSpeed)*Time.deltaTime), this.transform.position.y, ValZ);
			}
			m_previousTime = DateTime.Now;
		}
	}
}
